#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
CodeBuddy快速登录测试
直接使用提供的fetch请求参数进行登录测试
"""

import requests
from urllib.parse import unquote

def quick_login_test():
    """快速登录测试"""
    print("🚀 CodeBuddy快速登录测试")
    print("-" * 40)
    
    # 创建session
    session = requests.Session()
    
    # 登录URL
    login_url = "https://www.codebuddy.ai/auth/realms/copilot/login-actions/authenticate?session_code=xxjnB7YjKIl_ajufRD5EqDl_yPvKhdn94um5DLP2DUo&execution=fddd3124-02c5-4e2e-9e11-2849f14b804f&client_id=console&tab_id=6MTzVOJ10oI&client_data=eyJydSI6Imh0dHBzOi8vd3d3LmNvZGVidWRkeS5haS9jb25zb2xlL2FjY291bnRzLy5hcGlzaXgvcmVkaXJlY3QiLCJydCI6ImNvZGUiLCJzdCI6ImFhNzllMDRiOTYzNDBkMTRhZjcwNGZjYWFiYzM5NWZkIn0"
    
    # 请求头
    headers = {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "cache-control": "max-age=0",
        "content-type": "application/x-www-form-urlencoded",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "same-origin",
        "sec-fetch-user": "?1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
    }
    
    # 请求体数据
    data = "username=154hdgrkk7%40dpmurt.my&password=5gHpr0V%21&credentialId="
    
    # 显示解码后的凭据
    username = unquote("154hdgrkk7%40dpmurt.my")
    password = unquote("5gHpr0V%21")
    print(f"👤 用户名: {username}")
    print(f"🔑 密码: {password}")
    
    try:
        print("\n📡 发送登录请求...")
        response = session.post(
            login_url,
            headers=headers,
            data=data,
            allow_redirects=True,
            timeout=15
        )
        
        print(f"📊 状态码: {response.status_code}")
        print(f"🔗 最终URL: {response.url}")
        
        # 简单的成功判断
        if response.status_code == 200:
            if "console" in response.url and "account" in response.url:
                print("✅ 登录成功！")
                result = "成功"
            elif "error" in response.url.lower() or "login" in response.url.lower():
                print("❌ 登录失败")
                result = "失败"
            else:
                print("⚠️ 状态不确定")
                result = "不确定"
        else:
            print(f"❌ HTTP错误: {response.status_code}")
            result = "HTTP错误"
        
        # 显示cookies
        if session.cookies:
            print(f"\n🍪 获取到 {len(session.cookies)} 个cookies")
            for cookie in session.cookies:
                print(f"  - {cookie.name}")
        
        return result
        
    except Exception as e:
        print(f"❌ 请求异常: {e}")
        return "异常"

if __name__ == "__main__":
    result = quick_login_test()
    print(f"\n🎯 测试结果: {result}")